package com.zoffy.catdiarybackend.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zoffy.catdiarybackend.common.BaseResponse;
import com.zoffy.catdiarybackend.common.IdRequest;
import com.zoffy.catdiarybackend.common.PageRequest;
import com.zoffy.catdiarybackend.common.ResultUtils;
import com.zoffy.catdiarybackend.model.dto.article.*;
import com.zoffy.catdiarybackend.model.vo.ArticleTinyVo;
import com.zoffy.catdiarybackend.model.vo.ArticleVo;
import com.zoffy.catdiarybackend.model.vo.Permissions;
import com.zoffy.catdiarybackend.service.ArticleService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;
    @PostMapping("/publish")
    public BaseResponse<Long> articlePublish(@RequestBody ArticleCreateRequest articleCreateRequest, HttpServletRequest request){
        long articleId = articleService.createArticle(request, articleCreateRequest);
        return ResultUtils.success(articleId);
    }
    @PostMapping("/tiny/list")
    public BaseResponse<IPage<ArticleTinyVo>> articleTinyVoListByPage(@RequestBody PageRequest pageRequest){
        IPage<ArticleTinyVo> articleTinyVoIPage = articleService.listArticleTinyVoByPage(pageRequest);
        return ResultUtils.success(articleTinyVoIPage);
    }

    /**
     *
     * @param request HttpServletRequest
     * @param pageRequest pageResponse
     * @return 用户自己发布的文章简述信息
     */
    @PostMapping("/own/tiny/list")
    public BaseResponse<IPage<ArticleTinyVo>> articleOwnTinyVoListByPage(HttpServletRequest request,@RequestBody PageRequest pageRequest){
        IPage<ArticleTinyVo> articleTinyVoIPage = articleService.listOwnArticleTinyVoByPage(request,pageRequest);
        return ResultUtils.success(articleTinyVoIPage);
    }
    @PostMapping
    public BaseResponse<ArticleVo> getArticleById(HttpServletRequest request,@RequestBody IdRequest idRequest){
        ArticleVo articleVo = articleService.getArticleVoById(request,idRequest);
        return ResultUtils.success(articleVo);
    }
    @DeleteMapping
    public BaseResponse<Boolean> removeArticlesById(HttpServletRequest request,@RequestBody long[] ids){
        boolean res = articleService.deleteArticlesById(request, ids);
        return ResultUtils.success(res);
    }
    @PostMapping("/update")
    public BaseResponse<Boolean> updateArticle(@RequestBody ArticleUpdateRequest articleUpdateRequest,HttpServletRequest request){
        boolean res = articleService.updateArticleById(articleUpdateRequest, request);
        return ResultUtils.success(res);
    }
    @PostMapping("/permission/check")
    public BaseResponse<Boolean> checkPermission(@RequestBody CheckPermissionRequest checkPermissionRequest){
        boolean res = articleService.checkPermission(checkPermissionRequest);
        return ResultUtils.success(res);
    }
    @PostMapping("/permission/update")
    public BaseResponse<Boolean> updatePermission(@RequestBody PermissionUpdateRequest permissionUpdateRequest,HttpServletRequest request){
        boolean res = articleService.updatePermissionById(request,permissionUpdateRequest);
        return ResultUtils.success(res);
    }
    @PostMapping("/permission/")
    public BaseResponse<Permissions> getPermissionById(@RequestBody IdRequest idRequest,HttpServletRequest request){
        Permissions res = articleService.getPermissionsById(request,idRequest);
        return ResultUtils.success(res);
    }
    @PostMapping("/collaboration/update")
    public BaseResponse<Boolean> test(@RequestBody ArticleCollaborativeUpdateRequest articleCollaborativeUpdateRequest){
        articleService.updateCollaborationArticle(articleCollaborativeUpdateRequest);
        return ResultUtils.success();
    }
}
